package lpl.com.xyd.test.manage.factory;

import com.alibaba.fastjson.JSONObject;
import lpl.com.xyd.test.manage.common.factory.AbstractDomainFactory;
import lpl.com.xyd.test.manage.common.factory.AutowiredDomainFactory;
import lpl.com.xyd.test.manage.dao.UserMapperDAO;
import lpl.com.xyd.test.manage.domain.UserDomain;
import lpl.com.xyd.test.manage.dto.UserDTO;
import lpl.com.xyd.test.manage.param.UserParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class UserDomainFactory extends AbstractDomainFactory<UserDomain, UserParam> {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserDomainFactory.class);

    @Autowired
    private AutowiredDomainFactory autowiredDomainFactory;

    @Autowired
    private UserMapperDAO userMapperDAO;

    @Override
    public UserDomain create(UserParam param) {
        LOGGER.info("创建领域，domain:{}", JSONObject.toJSONString(param));
        UserDomain domain = autowiredDomainFactory.create(UserDomain.class);
        BeanUtils.copyProperties(param,domain);
        return domain;
    }

    @Override
    public UserDomain active(String domainId) {
        LOGGER.info("激活领域，domain:{}", domainId);
        UserDTO userDTO = new UserDTO();
        userDTO.setLoginName(domainId);
        UserDTO user = userMapperDAO.selectOne(userDTO);
        if (null != user){
            UserDomain domain = new UserDomain();
            BeanUtils.copyProperties(user,domain);
            return domain;
        }
        return null;
    }

    @Override
    public List<UserDomain> activeList(UserParam param) {
        LOGGER.info("批量激活领域，domain:{}", JSONObject.toJSONString(param));
//        UserDTO userDTO = new UserDTO();
//        BeanUtils.copyProperties(param,userDTO);
        List<UserDTO> userDTOS = userMapperDAO.selectByParam(param);
        if (null == userDTOS || userDTOS.size() <=0 ){
            return null;
        }
        List<UserDomain> userDomains = new ArrayList<>(userDTOS.size());
        userDTOS.forEach(v->{
            UserDomain userDomain = new UserDomain();
            BeanUtils.copyProperties(v,userDomain);
            userDomains.add(userDomain);
        });
        return userDomains;
    }

    @Override
    public Boolean store(UserDomain domain) {
        LOGGER.info("保存领域，domain:{}", JSONObject.toJSONString(domain));
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(domain,userDTO);
        int result = userMapperDAO.insert(userDTO);
        return result > 0;
    }

    @Override
    public Boolean reStore(UserDomain domain) {
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(domain,userDTO);
        return userMapperDAO.updateById(userDTO) > 0;
    }

    @Override
    public Boolean reStore(List<UserDomain> domains) {
        return null;
    }

    @Override
    public boolean delete(Long domainId) {
        return userMapperDAO.deleteByPrimaryKey(domainId) > 0;
    }

    @Override
    public boolean delete(List<Long> domainId) {
        return false;
    }
}
